package com.flyou.ccnucenter.ui.activity.gallery;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.adapter.LostFoundGalleryPhotoAdapter;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.ui.activity.henuBeside.lostFind.AddLostFoundActivity;
import com.flyou.ccnucenter.view.jazzyviewpager.JazzyViewPager;
import com.flyou.photopicker.model.PhotoInfo;

import java.util.ArrayList;

public class GalleryActivity extends BaseActivity1 implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private int currentPosition = 0;
    private ImageView IvDelete;
    private RelativeLayout RlDelete;
    private RelativeLayout RlBack;
    private TextView title;
    private JazzyViewPager viewPager;
    private ArrayList<PhotoInfo> photoLists;
    private LostFoundGalleryPhotoAdapter addGalleryPhotoAdapter;
    private  long lastClickTime;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_gallery);
        RlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        RlDelete = (RelativeLayout) findViewById(R.id.titlebar_rl_Right);
        IvDelete = (ImageView) findViewById(R.id.iv_right_image);
        title = (TextView) findViewById(R.id.titlebar_tv_title);
        viewPager = (JazzyViewPager) findViewById(R.id.jazzy_pager);


        RlDelete.setVisibility(View.VISIBLE);
        IvDelete.setImageResource(R.drawable.bbs_icon_delete);

    }

    @Override
    protected void initDate() {

        int position = getIntent().getIntExtra("position", 0);
        photoLists = getIntent().getParcelableArrayListExtra("photoLists");
        title.setText((position + 1) + "/" + photoLists.size());
        addGalleryPhotoAdapter = new LostFoundGalleryPhotoAdapter(GalleryActivity.this, photoLists);
        viewPager.setTransitionEffect(JazzyViewPager.TransitionEffect.Accordion);
        viewPager.setAdapter(addGalleryPhotoAdapter);
        viewPager.setCurrentItem(position);
        viewPager.setOnPageChangeListener(this);
    }

    @Override
    protected void setListener() {
        RlDelete.setOnClickListener(this);
        RlBack.setOnClickListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            Intent intent = new Intent(GalleryActivity.this, AddLostFoundActivity.class);
            intent.putParcelableArrayListExtra("photoList", photoLists);
            setResult(AddLostFoundActivity.GALLERYBACKOK, intent);
            finish();
            return false;

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.titlebar_rl_back:
                Intent intent = new Intent(GalleryActivity.this, AddLostFoundActivity.class);
                intent.putParcelableArrayListExtra("photoList", photoLists);
                setResult(AddLostFoundActivity.GALLERYBACKOK, intent);
                finish();
                break;
            case R.id.titlebar_rl_Right:
                if (isFastDoubleClick()) {
                    return;
                }
                if (photoLists.size()==1){
                    photoLists.remove(currentPosition);
                    Intent intentdeleteAll = new Intent(GalleryActivity.this, AddLostFoundActivity.class);
                    intentdeleteAll.putParcelableArrayListExtra("photoList", photoLists);
                    setResult(AddLostFoundActivity.GALLERYBACKOK, intentdeleteAll);
                    finish();
                    return;
                }
                photoLists.remove(currentPosition);
                addGalleryPhotoAdapter = new LostFoundGalleryPhotoAdapter(GalleryActivity.this, photoLists);
                viewPager.setAdapter(addGalleryPhotoAdapter);
                currentPosition=0;
                title.setText(1+"/"+(photoLists.size()));
                break;
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        currentPosition = position;
        title.setText((1 + position) + "/" + photoLists.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    private   boolean isFastDoubleClick() {

        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if ( 0 < timeD && timeD < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }
}
